package com.ibeeking.found.file.rest.service.office.services.configurers.implementations;

import com.ibeeking.found.file.rest.service.office.documentserver.managers.document.DocumentManager;
import com.ibeeking.found.file.rest.service.office.documentserver.models.filemodel.Document;
import com.ibeeking.found.file.rest.service.office.documentserver.models.filemodel.Permission;
import com.ibeeking.found.file.rest.service.office.documentserver.storage.FileStoragePathBuilder;
import com.ibeeking.found.file.rest.service.office.documentserver.util.file.FileUtility;
import com.ibeeking.found.file.rest.service.office.documentserver.util.service.ServiceConverter;
import com.ibeeking.found.file.rest.service.office.services.configurers.DocumentConfigurer;
import com.ibeeking.found.file.rest.service.office.services.configurers.wrappers.DefaultDocumentWrapper;
import com.ibeeking.found.file.service.common.dos.UserFileDO;
import com.ibeeking.nematos.utils.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Primary
public class DefaultDocumentConfigurer implements DocumentConfigurer<DefaultDocumentWrapper> {

    @Resource
    private DocumentManager documentManager;

    @Resource
    private FileStoragePathBuilder storagePathBuilder;

    @Resource
    private FileUtility fileUtility;

    @Resource
    private ServiceConverter serviceConverter;

    public void configure(Document document, DefaultDocumentWrapper wrapper){  // define the document configurer
        UserFileDO userFile = wrapper.getUserFile();

        String fileName = userFile.getFileName() + "." + userFile.getExtendName();  // get the fileName parameter from the document wrapper
        Permission permission = wrapper.getPermission();  // get the permission parameter from the document wrapper

        document.setTitle(fileName);  // set the title to the document config
        document.setUrl(wrapper.getPreviewUrl());  // set the URL to download a file to the document config
        document.setFileType(fileUtility.getFileExtension(fileName).replace(".",""));  // set the file type to the document config
        document.getInfo().setFavorite(wrapper.getFavorite());  // set the favorite parameter to the document config
        String uploadTimeStr = DateUtil.localDateTimeToStr(userFile.getUploadTime());
        String key =  serviceConverter.generateRevisionId(userFile.getUserFileId() + uploadTimeStr); // get the document key

        document.setKey(key);  // set the key to the document config
        document.setPermissions(permission);  // set the permission parameters to the document config
    }
}
